{extend name='extra@admin/content'}

{block name="button"}

{/block}

{block name="content"}

<form onsubmit="return false;" data-auto="true" method="post">
    <input type="hidden" name="goodsid" class="goodsidfin" value="{$goodsid}">
        <p class="help-block text-center well myaddsubmittogofi">
                    
        </p>
    <div class="cliczenjia ">增加一种原料</div>

    <div class="baocuncliczenjia " id="save">保存</div>
   
    <script>
        var  json = '<?php echo $matelist?>';
        require(['jquery'], function () {
            $('.cliczenjia').click(function(event) {
                var optionshtml =  addoption(json);
                $('.myaddsubmittogofi').append(optionshtml);
            });
            var optionshtml =  addoption(json);
              $('.myaddsubmittogofi').append(optionshtml);

            //json拼接
            function addoption(jsonht){
                jsonht = $.parseJSON(jsonht);
                var op ='<div class="eachfoblock">数量<input type="num" name="num" value="" >'
                op+='<select>';
                for (var i = jsonht.length - 1; i >= 0; i--) {
                     var heh = jsonht[i]
                     op+='<option value='+heh.id+'>'+heh.name;
                     op+='</option>'
                } 
                op+='备注<input type="text" name="remark" value="" >'
                op+='<span type="text" name="remark" value="" onclick="deletdiv(this)">删除</span>'
                op += '</select>';
                op+='</div>'
             return op;  
            }
            var urltoli = '<?php echo url("Recipe/add_pecipe_post") ?>'
            var goodsid = $('.goodsidfin').val()
            $('.baocuncliczenjia').on('click',function(event) {  
                        var input = $("div.eachfoblock input[name=num]");
                        var values = new Array();
                        for (var i = 0; i < input.length; i++) {
                            var obj={num:$(input[i]).val(),sele:$(input[i]).next().val(),remark:$(input[i]).next().next().val()}
                            values.push(obj);
                        }
                        console.dir(goodsid);
                        $.ajax({
                            url: urltoli,
                            type: 'POST',
                            dataType: 'json',
                            data: {param1: values,goodsid:goodsid},
                        })
                        .done(function(data) {
                            location.reload()
                        })
                        .fail(function() {
                            location.reload()
                        })
                        .always(function() {
                            location.reload()
                        });
            });           
        });

        function deletdiv($this){
            $this.parentNode.parentNode.removeChild($this.parentNode);
        }

        window.laydate.render({range: true, elem: '#range-date', format: 'yyyy/MM/dd'});

        
    </script>
</form>
<style type="text/css">
    .cliczenjia{
        width: 100px;
        height: 200px
    }
</style>
{/block}